/**  
 * All rights Reserved, Designed By www.tokenmaster.io
 * @Title:  Ai_XiaoK.java   
 * @Package org.aitrade.xiaok   
 * @author: 橘狮互联网科技有限公司 
 * @date:   2018年8月27日 上午3:05:57   
 * @version V1.0 
 * @Copyright: 2018 www.tokenmaster.io Inc. All rights reserved. 
 * 注意：本内容仅限于橘狮互联网科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package org.aitrade.xiaok;

import org.apache.log4j.Logger;

import com.tokenmaster.aitrade.common.account.Account;
import com.tokenmaster.aitrade.data.SystemDB;
import com.tokenmaster.aitrade.market.okex.IOkex_Parameter.Types_Enum;
import com.tokenmaster.aitrade.trade.boll.Index_Boll_Info;

/**
 * @ClassName: Ai_XiaoK
 * @Description: 交易机器人，小K
 * @author buxinyun
 * @date 2018年8月27日 上午3:05:57
 * 
 */
public class Ai_XiaoK {

	// 按15分钟的BOLL线进行买卖
	Logger logger = Logger.getLogger(this.getClass());

	private SystemDB systemDB = SystemDB.getInstance();

	private String symbol = "eth_usdt";

	private Types_Enum short_Type = Types_Enum.MIN15;

	private Types_Enum long_Type = Types_Enum.HOUR4;

	private Account account = new Account("小K", "2", "ETH");

	public void doWork() {

		// 获取15分钟Boll
		Index_Boll_Info short_Boll_Info = systemDB.getLastIndexBollInfo(symbol, short_Type.getKeyWord());

		// 获取4小时Boll
		Index_Boll_Info long_Boll_Info = systemDB.getLastIndexBollInfo(symbol, long_Type.getKeyWord());

		// 获取4小时BOLL线价差百分比
		double diff_Pec = long_Boll_Info.getUpPrice() / long_Boll_Info.getDownPrice() - 1;

		// 获取仓位 仓位 = 购入币花费 / （购入币花费 + 剩余钱）
		double accountPosition = account.getPosition();

		// 监测价格和布林线
		int position_Tag = checkShortBollPosition(short_Boll_Info);

		logger.info(short_Type.getKeyWord() + "布林线位置: " + short_Boll_Info.getPosition() + "  " + long_Type.getKeyWord()
				+ "布林线位置:" + long_Boll_Info.getPosition());

		double sell_position = 0;
		double avg_price = account.getAvg_price();

		// 检测买卖条件
		if (position_Tag == 2) {
			// 突破2倍上轨,有盈利每次卖出1/3,盈利超过10%,全仓卖出,亏损,卖出1/5
			logger.info(short_Type.getKeyWord() + "突破2倍上轨");

			// 盈利较大 10%，卖出仓位的1/3
			if (short_Boll_Info.getCurrentPrice() > avg_price * 1.14) {
				sell_position = 1;
			}
			// 有盈利
			else if (short_Boll_Info.getCurrentPrice() > avg_price * 1.07) {
				sell_position = 0.5;

			} else if (short_Boll_Info.getCurrentPrice() > avg_price) {
				sell_position = 0.25;
			}
			account.sellCoinByPosition(short_Boll_Info.getCurrentPrice(), sell_position, System.currentTimeMillis());
		} else if (position_Tag == 1) {
			logger.info(short_Type.getKeyWord() + "突破上轨");
			if (short_Boll_Info.getCurrentPrice() > avg_price * 1.14) {
				sell_position = 0.5;
			}
			// 有盈利
			else if (short_Boll_Info.getCurrentPrice() > avg_price * 1.07) {
				sell_position = 0.25;

			} else if (short_Boll_Info.getCurrentPrice() > avg_price) {
				sell_position = 0.125;
			}

			account.sellCoinByPosition(short_Boll_Info.getCurrentPrice(), sell_position, System.currentTimeMillis());
		} else if (position_Tag == -1) {
			logger.info(short_Type.getKeyWord() + "跌破下轨");

			// 买入直至1/5仓位
			if (accountPosition < 0.2)
				account.buyCoinByMoney(short_Boll_Info.getCurrentPrice(), 5000, System.currentTimeMillis());
			else if (accountPosition >= 0.2) {
				if (short_Boll_Info.getCurrentPrice() < avg_price * 0.95) {
					account.buyCoinByMoney(short_Boll_Info.getCurrentPrice(), 5000, System.currentTimeMillis());
				}
			}

		} else if (position_Tag == -2) {
			logger.info(short_Type.getKeyWord() + "跌破双倍下轨");
			// 买入仓位1/2
			if (accountPosition < 0.5)
				account.buyCoinByMoney(short_Boll_Info.getCurrentPrice(), 10000, System.currentTimeMillis());
			else if (accountPosition >= 0.5) {
				if (short_Boll_Info.getCurrentPrice() < avg_price * 0.95) {
					account.buyCoinByMoney(short_Boll_Info.getCurrentPrice(), 10000, System.currentTimeMillis());
				}
			}

		} else {
			logger.info(short_Type.getKeyWord() + "中轨附近,当前布林线位置: " + short_Boll_Info.getPosition());
		}

	}

	// 突破上轨标记
	private boolean upBreak = false;

	private boolean upBreak_Double = false;

	// 突破下轨标记
	private boolean downBreak = false;

	private boolean downBreak_Double = false;

	/**
	 * @param index_Boll_Info
	 * @return 0 无 1 突破上轨, 2 突破2倍上轨, -1 跌破下轨, -2 跌破 2倍下轨
	 */
	private int checkShortBollPosition(Index_Boll_Info index_Boll_Info) {

		int result = 0;

		// 突破2倍上轨
		if (index_Boll_Info.getPosition() == 6) {

			if (upBreak_Double == false) {
				result = 2;
			}
			upBreak_Double = true;
			upBreak = true;
			downBreak = false;
			downBreak_Double = false;
		}
		// 突破上轨
		else if (index_Boll_Info.getPosition() == 5) {

			if (upBreak == false) {
				result = 1;
			}

			upBreak_Double = false;
			upBreak = true;
			downBreak = false;
			downBreak_Double = false;

		}
		// 中轨上方
		else if (index_Boll_Info.getPosition() == 4) {
			upBreak_Double = false;
			downBreak = false;
			downBreak_Double = false;
		}
		// 中轨下方
		else if (index_Boll_Info.getPosition() == 3) {
			upBreak_Double = false;
			upBreak = false;
			downBreak_Double = false;
		}
		// 跌破下轨
		else if (index_Boll_Info.getPosition() == 2) {

			if (downBreak == false) {
				result = -1;
			}

			upBreak_Double = false;
			upBreak = false;
			downBreak = true;
			downBreak_Double = false;
		}
		// 跌破2倍下轨
		else if (index_Boll_Info.getPosition() == 1) {
			if (downBreak_Double == false) {
				result = -2;
			}
			upBreak_Double = false;
			upBreak = false;
			downBreak = true;
			downBreak_Double = true;
		}

		return result;
	}

}
